#coding: utf-8

from base import BaseRender

class ProtovisRender(BaseRender):
    def render(self, f):
        nodes = ',\n        '.join(
          ['{nodeName: "%s", group: "%s"}' % (name, props['subsistema']) for name, props in self.meta.nodes.items()])

        indices = dict([[v,k] for k,v in enumerate(self.meta.nodes.keys())])
        
        links = []
        for dsts in self.meta.networks.values():
            if dsts == []: continue
            for src, dst in dsts:
                links += ['{source:%d, target:%d}' % (indices[src], indices[dst])]

        links = ',\n        '.join(links)

        s = '''
var dependencies = {
    nodes: [
        %(nodes)s
    ],
    links: [
        %(links)s
    ]
};
        ''' % locals()

        f.write(s)